package com.qf.boot220301.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import com.google.code.kaptcha.Constants;
import com.qf.boot220301.vo.ResultVo;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private Kaptcha kaptcha;

   /* @RequestMapping("/login")
    public ResultVo login(String captchar, HttpSession session){
        final String attribute = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if(attribute != null){
            if(attribute.equals(captchar)){
                return ResultVo.ok();
            }
        }
        return ResultVo.error("验证码错误,请从新登入!");
    }*/
    @RequestMapping("/login")
    public ResultVo login(String captchar){
        try {
            kaptcha.validate(captchar);
        }catch (Exception e){
            if (e instanceof KaptchaIncorrectException) {
                return ResultVo.error("验证码不正确");
            } else if (e instanceof KaptchaNotFoundException) {
                return ResultVo.error("验证码未找到");
            } else if (e instanceof KaptchaTimeoutException) {
                return ResultVo.error("验证码过期");
            } else {
                return ResultVo.error("验证码渲染失败");
            }
        }

        return ResultVo.ok();
    }
}
